package model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author emidio
 */
public class acessoBanco {

    private String url="jdbc:mysql://localhost:3306/cadastro";
    private String driver="com.mysql.jdbc.Driver";
    private String user="root";
    private String senha="8eo6f551v";
    private Connection conn = null;

    public acessoBanco() {
    }

    public Connection getConnexao() throws Exception{
        try{
            if(conn==null){
                Class.forName(driver);
                conn = DriverManager.getConnection(url,user,senha);
            }
        }catch(Exception e){
            throw new Exception(e);
        }
        return conn;
    }

    public Statement getStatement() throws SQLException, Exception {
        return this.getConnexao().createStatement();
    }

    public PreparedStatement getPrepareStatemen(String sql) throws SQLException, Exception{
        return this.getConnexao().prepareStatement(sql);
    }

    public void fecharTudo() throws Exception{
        if(conn!=null){
            this.getConnexao().close();
        }
    }
}
